# _*_ coding:utf-8 _*_
# @Time  : 2023.08.24
# @Author: zizlee
from fastapi import APIRouter, Query, HTTPException
from utils import datetime_utils
from response import APIResponse
from .doctor_tools import ExDoctorDatabase, CloseStatistician


class CloseStatistics:
    def __init__(self, fn, start: str, end: str):
        self.fn = fn
        self.start = datetime_utils.auth_yyyy_mm_dd(start, obj=True)
        self.end = datetime_utils.auth_yyyy_mm_dd(end, obj=True)
        if not self.start:
            raise HTTPException(status_code=422, detail='query参数`start`格式错误,接受yyyy-mm-dd类型')
        if not self.end:
            raise HTTPException(status_code=422, detail='query参数`end`格式错误,接受yyyy-mm-dd类型')
        self.start_integer = self.start.strftime('%Y%m%d')
        self.end_integer = self.end.strftime('%Y%m%d')

    def close_position_overview(self):
        database = ExDoctorDatabase(fund_num=self.fn, start=self.start_integer, end=self.end_integer)
        close_list = database.query_close_position_data()
        if len(close_list) < 1:
            data = {'overview': []}
        else:
            close_statistician = CloseStatistician(close_list)
            data = close_statistician.close_profit_overview(filename_prefix=self.fn, doc=True)
        return APIResponse.success(data=data)

    def close_position_bs_profit(self):
        database = ExDoctorDatabase(fund_num=self.fn, start=self.start_integer, end=self.end_integer)
        close_list = database.query_close_position_data()
        if len(close_list) < 1:
            data = {'data': {}, 'img_count_pie': '', 'img_fund_pie': ''}
        else:
            close_statistician = CloseStatistician(close_list)
            data = close_statistician.create_pupil_image_pie(filename_prefix=self.fn, fig_size=(5.5, 3.75), xtick_count=12)  # 多空分析
        return APIResponse.success(data=data)

    def close_position_variety_profit(self):
        # 品种盈亏排名和单位利交比排名
        database = ExDoctorDatabase(fund_num=self.fn, start=self.start_integer, end=self.end_integer)
        close_list = database.query_close_position_data()
        if len(close_list) < 1:
            data = {'rank_txt': '', 'rank_img': '', 'lj_img': '', 'lj_txt': ''}
        else:
            close_statistician = CloseStatistician(close_list)
            data = close_statistician.create_variety_profit_image(filename_prefix=self.fn, fig_size=(5.5, 3.75), xtick_count=12)
        return APIResponse.success(data=data)

    def close_position_period_type(self):
        database = ExDoctorDatabase(fund_num=self.fn, start=self.start_integer, end=self.end_integer)
        close_list = database.query_close_position_data()
        if len(close_list) < 1:
            data = {'txt': '', 'data': {}, 'img_file': ''}
        else:
            close_statistician = CloseStatistician(close_list)
            data = close_statistician.create_exchange_period_image(filename_prefix=self.fn, fig_size=(5.5, 3.75), xtick_count=12) # 交易周期类型分析
        return APIResponse.success(data=data)


ex_close_api = APIRouter()


@ex_close_api.get('/overview/')  # 平仓盈利与亏损
def close_position_overview(fn: str = Query(...), start: str = Query(...), end: str = Query(...)):
    return CloseStatistics(fn=fn, start=start, end=end).close_position_overview()


@ex_close_api.get('/bsProfit/')  # 多空盈亏分析
def close_position_bs_profit(fn: str = Query(...), start: str = Query(...), end: str = Query(...)):
    return CloseStatistics(fn=fn, start=start, end=end).close_position_bs_profit()


@ex_close_api.get('/varietyProfit/')  # 品种盈亏分析
def close_position_variety_profit(fn: str = Query(...), start: str = Query(...), end: str = Query(...)):
    return CloseStatistics(fn=fn, start=start, end=end).close_position_variety_profit()


@ex_close_api.get('/periodType/')  # 交易周期类型
def close_position_period_type(fn: str = Query(...), start: str = Query(...), end: str = Query(...)):
    return CloseStatistics(fn=fn, start=start, end=end).close_position_period_type()

